home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / mathstud.zip / MEDIAN.M < prev    next >
Text File  |  1994-01-23  |  764b  |  29 lines

  1. function [Y, I] = median(X)
  2. %[Y, I] = median(X)
  3. %The median find the median element of X
  4. %if X is a vector, the median element and its index is returned.
  5. %if X is a matrix, the median function operates columnwise, returning
  6. %a row vector with elements corresponding to the median of each column
  7. %in X and a row vector whose elements are the index of the median in
  8. %each column.
  9.  
  10. %       S.Halevy 7/31/92
  11. %       Copyright (c) 1992 by the MathWizards
  12.  
  13. if (nargin ~= 1)
  14.    error('median function needs 1 argument');
  15. end
  16.  
  17. if ( isrow(X) )
  18.    X = X';
  19. end
  20. [r, c] = size(X);
  21. [sX, sI] = sort(X);
  22. Y = zeros(1, c);
  23. I = Y;
  24. for iter=1:c
  25.    row = floor( max( size(sI(:,iter)) ) / 2 + 1 );
  26.    Y(iter) = sX(row, iter);
  27.    I(iter) = sI(row, iter);
  28. end
  29.